package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.base.entity.GermchitManagement;
import com.fishery.base.entity.GermchitOrder;
import com.fishery.base.entity.vo.GermchitOrderShowVo;
import com.fishery.base.mapper.GermchitManagementMapper;
import com.fishery.base.mapper.GermchitOrderMapper;
import com.fishery.base.service.GermchitOrderService;
import com.fishery.base.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

@Service
public class GermchitOrderServiceImpl extends ServiceImpl<GermchitOrderMapper, GermchitOrder> implements GermchitOrderService {

	@Autowired
	private GermchitOrderMapper germchitOrderMapper;

	@Autowired
	private GermchitManagementMapper germchitManagementMapper;

	@Autowired
	private ExecutorService threadPool;

	@Override
	@Transactional
	public boolean updateOrderStatus(String orderId) {
		// 查询订单内容
		GermchitOrder order = germchitOrderMapper.selectById(orderId);
		if (order == null) {
			return false;
		}
		// 修改订单状态
		order.setStatus(1);
		// 将订单更新到数据库中
		germchitOrderMapper.updateById(order);
		// 将种苗入库（1、可能库中有该种苗，2、库中不存在该种苗）
		// 查询是否已经存在该种苗
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("germchit_id", order.getGermchitId());
		wrapper.eq("base_id", order.getBaseId());
		GermchitManagement germchitRecord = germchitManagementMapper.selectOne(wrapper);
		// 判断是否存在
		if (germchitRecord == null) {
			// 不存在，则将种苗直接入库
			germchitRecord = new GermchitManagement();
			germchitRecord.setGermchitNumber(order.getPurchaseAmount());
			germchitRecord.setGermchitSurplusNumber(order.getPurchaseAmount());
			germchitRecord.setBaseId(order.getBaseId());
			germchitRecord.setGermchitId(order.getGermchitId());
			germchitManagementMapper.insert(germchitRecord);
		} else {
			// 存在，增加该种苗的库存
			germchitRecord.setGermchitNumber(germchitRecord.getGermchitNumber() + order.getPurchaseAmount());
			germchitRecord.setGermchitSurplusNumber(germchitRecord.getGermchitSurplusNumber() + order.getPurchaseAmount());
			germchitManagementMapper.updateById(germchitRecord);

		}
		return true;
	}

	@Override
	public Page<GermchitOrderShowVo> pageOrders(String baseId, int size, int current) {
		Page<GermchitOrderShowVo> page = new Page<>(current, size);
		Future<List<GermchitOrderShowVo>> future = threadPool.submit(() ->
				germchitOrderMapper.selectGermchitOrderShowVoPage(baseId, (current - 1) * size, size));
		int total = germchitOrderMapper.selectTotal(baseId);
		page.setTotal(total);
		page.setPages(total / size + (total % size == 0 ? 0 : 1));
		try {
			page.setRecords(future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public GermchitOrderShowVo getGermchitOrderShowVoById(String orderId) {
		return germchitOrderMapper.selectGermchitOrderShowVoById(orderId);
	}

	@Override
	public void getGermchitOrdersDataByExcel(HttpServletResponse response, String baseId) {
		ExcelUtil.write(response, "基地种苗订单", GermchitOrderShowVo.class, "种苗订单", germchitOrderMapper.selectAllGermchitOrderShowVos(baseId));
	}
}
